/**
 * Created by yunpeng on 2014/12/4.
 */
var Table = require('../utils/Table').Table;

var Citys = function(province, city, firstletter) {
    this.province = province;
    this.city = city;
    if (typeof this.save != 'function') {//仅在第一次调用的初始化
        Citys.prototype.save = function(){
            var sql = 'insert into citys set ?';
            db.execQuery(sql,this,function(result){
                console.log(result);
            });
        };
    }
    if (typeof this.getCityList!='function'){
        Citys.prototype.getCityList = function(id,callback){
            /*new Table('citys').queryAll(function(err,result){
                if(err){
                    callback(null);
                }else{
                    callback(result);
                }
            })*/
            var sql = "select * from citys ";// CONVERT( province USING gbk ) COLLATE gbk_chinese_ci ASC"
            if(id){
                sql+="where id = "+id;
            }
            new Table().executeSql(sql,[],function(err, result){
                if(err){
                    debug(err);
                    callback(['']);
                }else{
                    callback(result);
                }
            })
        }

    }

    /**
     * 获取省份
     */
    if (typeof this.getProvince != 'function') {
        Citys.prototype.getProvince = function (callback) {
            new Table().executeSql("SELECT l.province FROM citys l group by CONVERT( province USING gbk ) COLLATE gbk_chinese_ci ASC", [], function(err, result){
                if(err){
                    callback([]);
                }else{
                    callback(result);
                }
            });
        }
    }


    if (typeof this.getCitysByProvince != 'function') {
        Citys.prototype.getCitysByProvince = function (province,callback) {
            new Table().executeSql("SELECT * FROM citys where province=?", [province], function(err, result){
                if(err){
                    callback([]);
                }else{
                    callback(result);
                }
            });
        }
    }
}
exports.Citys = Citys;